/*
 * dt.cpp
 *
 *  Created on: 2010-11-17
 *      Author: zhzuo
 */

#include "dt.h"
#include <QFile>
#include <QTextStream>
#include <QTextDocument>
#include <QTime>
#include <QDomDocument>
#include <QDomElement>

bool dt::FileExist(const QString& fp){
	QFile file(fp);
	bool b=true;
	if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
		b=false;
	}
	file.close();
	return b;
}

void dt::Start(){
	Params::InitParams();
	QFile* file;
	QTextStream* out;
	if(!dt::FileExist(_LOG_)){
		file=new QFile(_LOG_);
		if(!file->open(QIODevice::WriteOnly | QIODevice::Text)){
			return;
		}
		out=new QTextStream(file);
		*out<<"start:"<<QTime::currentTime().toString(_TIME_FORMAT_)<<endl;
		file->close();
	}
	if(!dt::FileExist(_CONFIG_XML_)){
		file=new QFile(_CONFIG_XML_);
		if(!file->open(QIODevice::WriteOnly | QIODevice::Text)){
			return;
		}
		out=new QTextStream(file);
		out->flush();
		*out<<_XML_HEADER_<<endl;
		*out<<"<data>"<<endl;
		*out<<"<userid></userid>"<<endl;
		*out<<"<userpwd></userpwd>"<<endl;
		*out<<"<autologin>0</autologin>"<<endl;
		*out<<"<rememberpwd>0</rememberpwd>"<<endl;
		*out<<"</data>"<<endl;
		file->close();
	}
	dt::ReadConfigXML();
}

void dt::Set(const QString& k,const QString& v){
	(*dt_cache)[k]=v;
}

QString dt::Get(const QString& k,const QString& v){
	if((dt_cache->find(k))==dt_cache->end()){
		QString r(v);
		return r;
	}
	return (*dt_cache)[k];
}

void dt::ReadConfigXML(){
	QFile* file = new QFile(_CONFIG_XML_);
	if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
		return;
	}
	QDomDocument doc("mydocument"); 
	if (!doc.setContent(file)) 
	{          
		return;      
	}     
	QDomElement docElem = doc.documentElement(); 
	QString rootTag = docElem.tagName(); 
	QDomElement qde=docElem.firstChildElement(_USERID_);
	dt::Set(_USERID_,qde.text());
	qde=docElem.firstChildElement(_USERPWD_);
	dt::Set(_USERPWD_,qde.text());
	qde=docElem.firstChildElement(_AUTO_LOGIN_);
	dt::Set(_AUTO_LOGIN_,qde.text());
	qde=docElem.firstChildElement(_REMEMBER_PWD_);
	dt::Set(_REMEMBER_PWD_,qde.text());
	file->close();
}

QString dt::UserID()
{
	return dt::Get(_USERID_);
}

QString dt::UserGuid()
{
	return dt::Get(_GUID_);
}

QString dt::Token()
{
	return dt::Get(_TOKEN_);
}


QString dt::UserPwd()
{
	return dt::Get(_USERPWD_);
}

bool dt::AutoLogin(){
	return dt::Get(_AUTO_LOGIN_)=="1";
}

bool dt::RememberPwd(){
	return dt::Get(_REMEMBER_PWD_)=="1";
}

void dt::SetConfigXML(const QString& uid,const QString& pwd,const QString& al,const QString& rp){
	QFile* file;
	QTextStream* out;
	file=new QFile(_CONFIG_XML_);
	if(!file->open(QIODevice::Truncate | QIODevice::Text)){
		return;
	}
	file->close();
	file=new QFile(_CONFIG_XML_);
	if(!file->open(QIODevice::WriteOnly | QIODevice::Text)){
		return;
	}
	//QString xx=escape(uid);
	out=new QTextStream(file);
	out->flush();
	*out<<_XML_HEADER_<<endl;
	*out<<"<data>"<<endl;
	*out<<"<userid>"<<Qt::escape(uid)<<"</userid>"<<endl;
	*out<<"<userpwd>"<<Qt::escape(pwd)<<"</userpwd>"<<endl;
	*out<<"<autologin>"<<al<<"</autologin>"<<endl;
	*out<<"<rememberpwd>"<<rp<<"</rememberpwd>"<<endl;
	*out<<"</data>"<<endl;
	file->close();
}

void dt::WriteLog(const QString& log){
	QFile* file;
	QTextStream* out;
	file=new QFile(_LOG_);
	if(!file->open(QIODevice::WriteOnly | QIODevice::Text)){
		return;
	}
	out=new QTextStream(file);
	*out<<log<<"\t"<<QTime::currentTime().toString(_TIME_FORMAT_)<<endl;
	file->close();
}
